package com.i.template.controller;

import cn.hutool.core.bean.BeanUtil;
import com.i.template.constant.ResponseResult;
import com.i.template.domain.MusicDO;
import com.i.template.domain.vo.MusicVO;
import com.i.template.service.IMusicService;
import com.i.template.util.UserHolder;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lewis
 * @type class
 * @description
 * @since 2024/8/2
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/music")
@Tag(name = "音乐管理", description = "用于管理音乐信息")
public class MusicController {
    private final IMusicService musicService;
    private final StringRedisTemplate redisTemplate;
    @GetMapping
    public ResponseResult<List<MusicVO>> listByMusic() {
        List<MusicDO> musicDOList = musicService.list();
        return ResponseResult.ok(BeanUtil.copyToList(musicDOList, MusicVO.class));
    }
    @GetMapping("/{mid}")
    public ResponseResult<MusicVO> getMusicById(@PathVariable Long mid) {
        MusicDO musicDO = musicService.getById(mid);
        return ResponseResult.ok(BeanUtil.copyProperties(musicDO,MusicVO.class));
    }
    @PostMapping
    public ResponseResult<Boolean> saveMusic(@RequestBody MusicVO musicVO) {
        return ResponseResult.judge(musicService.saveMusic(BeanUtil.copyProperties(musicVO,MusicDO.class)));
    }
    @PutMapping
    public ResponseResult<Boolean> updateMusicById(@RequestBody MusicVO musicVO) {
        return ResponseResult.judge(musicService.updateById(BeanUtil.copyProperties(musicVO,MusicDO.class)));
    }
    @DeleteMapping("/{mid}")
    public ResponseResult<Boolean> deleteMusicById(@PathVariable Long mid) {
        return ResponseResult.judge(musicService.removeById(mid));
    }
    @PostMapping("/list/{id}")
    public ResponseResult<Boolean> saveMusicToList(@PathVariable("id") Long musicId) {
        Long userId = UserHolder.getUser();
        return ResponseResult.judge(musicService.saveMusicToList(userId,musicId));
    }
    @GetMapping("/list/{id}")
    public ResponseResult<List<MusicVO>> listMusicListByUserId(@PathVariable Long id) {
        return ResponseResult.ok(BeanUtil.copyToList(musicService.queryMusicListByUserId(id),MusicVO.class));
    }
}
